package com.kasax.okhttp;

import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.kasax.httpclient.beans.Request;
import com.kasax.httpclient.beans.Response;
import com.kasax.httpclient.core.KOkHttpClient;
import com.kasax.httpclient.core.inters.Call;
import com.kasax.httpclient.core.inters.Callback;
import com.kasax.httpclient.interceptors.HttpLoggingInterceptor;
import com.kasax.httpclient.utils.LogUtils;

import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        KOkHttpClient KOkHttpClient = new KOkHttpClient.Builder()
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .conenctTimeout(30, TimeUnit.SECONDS)
                .callTimeout(30, TimeUnit.SECONDS)
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
        Request request = new Request.Builder()
                .url("https://wanandroid.com/harmony/index/json")
//                .url("http://www.baidu.com")
                .get().build();
        Call call = KOkHttpClient.newCall(request);
//        Response response = call.execute(request);
        call.enqueue(new Callback() {
            @Override
            public void onSuccess(Call call, Response response) {
                LogUtils.i(getClass(), response.getBody());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, response.getBody(), Toast.LENGTH_LONG).show();
                    }
                });
            }

            @Override
            public void onFailure(Call call, Exception exception) {

            }
        });


    }
}